import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_tableview/flutter_tableview.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';


class WaitPayRecord extends StatefulWidget {
  final String name;

  WaitPayRecord({Key key, this.name});

  @override
  _WaitPayRecordState createState() => _WaitPayRecordState();
}

class _WaitPayRecordState extends State<WaitPayRecord> {
  //触发刷新和加载动作的controller
  EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colours.white,
          centerTitle: true,
          iconTheme: IconThemeData(color: Colours.app_words),
          title: Text(
            '待缴' + widget.name,
            style: TextStyle(fontSize: 18, color: Colours.app_words),
          ),
        ),
        body: Container(
          color: Colours.gray_ef,
          width: double.infinity,
          height: double.infinity,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                color: Colours.white,
                height: AppSize.height(98),
                width: double.infinity,
                child: Row(
                  children: <Widget>[
                    Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(left: AppSize.sp(32)),
                      child: Icon(),
                    ),
                    Container(
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(left: AppSize.sp(20)),
                        child: Text(
                          widget.name,
                          style: TextStyle(
                              fontSize: AppSize.sp(30),
                              fontWeight: FontWeight.w600,
                              color: Colours.color_313131),
                        )),
                  ],
                ),
              ),
              Expanded(
                child: FlutterTableView(
                  sectionCount: sectionCount,
                  rowCountAtSection: _rowCountAtSection,
                  sectionHeaderBuilder: _sectionHeaderBuilder,
                  cellBuilder: _cellBuilder,
                  sectionHeaderHeight: _sectionHeaderHeight,
                  cellHeight: _cellHeight,
                  listViewFatherWidgetBuilder:
                      (BuildContext context, Widget listView) {
                    return EasyRefresh(
                      controller: _controller,
//                      header: MaterialHeader(),
//                      footer: MaterialFooter(),
                      onRefresh: () async {},
                      onLoad: () async {
                        _controller.finishLoad(success: true, noMore: false);
                      },
                      child: listView,
                    );
                  },
                ),
              )
            ],
          ),
        ));
  }

  Image Icon() {
    if ("进场缴费" == widget.name) {
      return Image.asset(
        'images/icon_jinchang_shoufei.png',
        fit: BoxFit.cover,
      );
    } else if ("罚款" == widget.name) {
      return Image.asset(
        'images/icon_fakuan.png',
        fit: BoxFit.cover,
      );
    } else if ("水费" == widget.name) {
      return Image.asset(
        'images/icon_water_fee.png',
        fit: BoxFit.cover,
      );
    } else if ("电费" == widget.name) {
      return Image.asset(
        'images/icon_dianfei.png',
        fit: BoxFit.cover,
      );
    } else if ("物业费" == widget.name) {
      return Image.asset(
        'images/icon_wuyefei.png',
        fit: BoxFit.cover,
      );
    } else if ("卫生费" == widget.name) {
      return Image.asset(
        'images/icon_weishengfei.png',
        fit: BoxFit.cover,
      );
    } else if ("临时摊位费" == widget.name) {
      return Image.asset(
        'images/icon_linshi_tanweifei.png',
        fit: BoxFit.cover,
      );
    } else if ("其他费用" == widget.name) {
      return Image.asset(
        'images/icon_other_fee.png',
        fit: BoxFit.cover,
      );
    } else if ("固定摊位租金" == widget.name) {
      return Image.asset(
        'images/icon_guding_tanweizujin.png',
        fit: BoxFit.cover,
      );
    } else if ("固定仓库租金" == widget.name) {
      return Image.asset(
        'images/icon_guding_cangkuzujin.png',
        fit: BoxFit.cover,
      );
    } else if ("保证金" == widget.name) {
      return Image.asset(
        'images/icon_baozhengjin.png',
        fit: BoxFit.cover,
      );
    }
  }

  // 有多少个组
  int sectionCount = 6;

  // 每组有多少行（每组有多少个cell item）
  int _rowCountAtSection(int section) {
    if (section == 0) {
      return 5;
    } else if (section == 1) {
      return 10;
    } else if (section == 2) {
      return 10;
    } else if (section == 3) {
      return 10;
    } else if (section == 4) {
      return 10;
    } else {
      return 20;
    }
  }

  // 创建每组的 section header widget
  Widget _sectionHeaderBuilder(BuildContext context, int section) {
    return InkWell(
      onTap: () {
        print('click section header. -> section:$section');
      },
      child: Container(
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.only(left: 16.0),
        color: Colours.gray_ef,
        child: Text('${section + 1}' + "月"),
      ),
    );
  }

  // 根据 section 和 row, 创建对应的 cell item widget
  Widget _cellBuilder(BuildContext context, int section, int row) {
    return InkWell(
      onTap: () {
        print('click cell item. -> section:$section row:$row');
      },
      child: Container(
        padding: EdgeInsets.only(left: 16.0),
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
            color: Colours.white,
            border: Border(
                bottom: BorderSide(
              color: Colours.color_dcdcdc,
            ))),
        child: Row(
          children: <Widget>[
            Text("10月24日" + ": ",
                style: TextStyle(
                    fontSize: AppSize.sp(30), color: Colours.color_898989)),
            Text("8958元",
                style: TextStyle(
                    fontSize: AppSize.sp(30), color: Colours.color_313131)),
            Spacer(),
            Container(
              width: AppSize.width(121),
              height: AppSize.height(52),
              decoration: BoxDecoration(
                color: Colours.color_dd4b39,
                borderRadius: BorderRadius.all(Radius.circular(AppSize.sp(4))),
              ),
              child: Text("支付",
                  style: TextStyle(
                      color: Colours.white, fontSize: AppSize.sp(26))),
              alignment: Alignment.center,
              margin: EdgeInsets.only(right: AppSize.sp(32)),
            )
          ],
        ),
      ),
    );
  }

  // section header widget 的高度
  double _sectionHeaderHeight(BuildContext context, int section) {
    return AppSize.height(74);
  }

  // cell item widget 的高度
  double _cellHeight(BuildContext context, int section, int row) {
    return AppSize.height(98);
  }
}
